package org.mixed;

import org.mixed.config.MixProperties;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.boot.CommandLineRunner;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.EnableAutoConfiguration;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.autoconfigure.jdbc.DataSourceAutoConfiguration;
import org.springframework.boot.context.properties.EnableConfigurationProperties;

import java.net.InetAddress;

@SpringBootApplication
@EnableConfigurationProperties(MixProperties.class)
@EnableAutoConfiguration(exclude = {DataSourceAutoConfiguration.class})
public class MixApplication implements CommandLineRunner {
    @Value("${server.port:8080}")
    private int port;
    @Value("${server.servlet.context-path:}")
    private String contextPath;
    @Value("${mix.prefix:/mix/web}")
    private String prefix;

    public static void main(String[] args) {
        SpringApplication.run(MixApplication.class, args);
    }

    @Override
    public void run(String... args) throws Exception {
        System.out.println("********************************************当前服务相关地址********************************************");
        String ip = InetAddress.getLocalHost().getHostAddress();
        String schema = "http://";
        String localPath = String.format("localhost:%s/%s/", port, contextPath);
        String externPath = String.format("%s:%s/%s/", ip, port, contextPath);
        String webPath = String.format("%s:%s/%s/%s/index.html", ip, port, contextPath, prefix);
        System.out.printf(
                """
                    服务启动成功，mix-script已内置启动! Access URLs:
                    接口本地地址: \t%s
                    接口外部地址: \t%s
                    接口配置平台: \t%s
                """
                , schema + localPath.replaceAll("/+", "/")
                , schema + externPath.replaceAll("/+", "/")
                , schema + webPath.replaceAll("/+", "/")
        );
        System.out.println("********************************************当前服务相关地址********************************************");
    }
}
